package com.wbs.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import com.wbs.resutil.Result;
import com.wbs.resutil.ResultEnums;
import darabonba.core.client.ClientOverrideConfiguration;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 芝梦
 * @Description: 发送短信  服务
 * @DateTime: 2024/2/22 16:44
 */
@RestController
@RequestMapping("send")
public class SendSmsController{

    @Autowired
    private RedisTemplate redisTemplate;


    @Value("${ali.accessKeyId}")
    private String accessKeyId;
    @Value("${ali.accessKeySecret}")
    private String accessKeySecret;
    @Value("${ali.region}")
    private String region;
    @Value("${ali.setEndpointOverride}")
    private String setEndpointOverride;
    @Value("${ali.signName}")
    private String signName;
    @Value("${ali.templateCode}")
    private String templateCode;

    @GetMapping("mdssage")
    public Result message(String phone){
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        AsyncClient client = AsyncClient.builder()
                .region(region) // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(setEndpointOverride)
                )
                .build();


        //从redis中获取验证码改手机号验证码
        String captcha = (String) redisTemplate.opsForValue().get("captcha"+phone);
        if (captcha==null || captcha.isEmpty()){
            //生成随机验证码 hutool提供
            captcha = RandomUtil.randomNumbers(6);
            //存入该手机号redis验证码 并设置1分钟有效期
            redisTemplate.opsForValue().set("captcha"+phone,captcha,10,TimeUnit.MINUTES);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",captcha);
        String jsonString = JSONObject.toJSONString(jsonObject);
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName(signName)
                .templateCode(templateCode)
                .phoneNumbers(phone)
                .templateParam(jsonString)
                .build();

        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = null;
        try {
            resp = response.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        System.out.println(new Gson().toJson(resp));

        client.close();
        return Result.RESPONSE(ResultEnums.SEND_SUCCESS);
    }
}
